import socket


def main():
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_server_socket.bind(("192.168.159.1", 9000))
    tcp_server_socket.listen(128)
    new_client_socket, client_addr = tcp_server_socket.accept()
    print("【%s】 请求下载文件" % str(client_addr))

    file_content = None
    # 读取文件
    try:
        f = open("download.txt", "rb")  # 以二进制方式读取文件，直接使用socket写不用转码
        file_content = f.read()
        f.close()
    except Exception as ret:
        print("读取文件失败")

    # 使用socket写回文件
    if file_content:
        new_client_socket.send(file_content)
    tcp_server_socket.close()


if __name__ == '__main__':
    main()
